import commonConfig from './commonConfig'
import store from '../../store'

const status = commonConfig.options.status;

const statisticsMethods = {
    echartsOption: {
        line: {
            x: {
                axisTick: {
                    show: false
                },
                axisLabel: {
                    color: '#87603F'
                },
                axisLine: {
                    lineStyle: {
                        type: 'solid',
                        color: '#CFB8A1'
                    }
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        type: 'dotted',
                        color: 'rgba(175, 137, 98, .6)'
                    }
                },
                splitNumber: 11,
                minorSplitLine: {
                    show: true,
                    lineStyle: {
                        type: 'dotted',
                        color: 'rgba(175, 137, 98, .2)'
                    }
                }
            },
            y: {
                axisTick: {
                    show: false
                },
                axisLabel: {
                    color: '#87603F'
                },
                axisLine: {
                    lineStyle: {
                        type: 'solid',
                        color: '#CFB8A1'
                    }
                },
                splitLine: {
                    lineStyle: {
                        color: 'rgba(175, 137, 98, .6)',
                        type: 'dotted'
                    }
                }
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross'
                },
                backgroundColor: 'rgba(175, 137, 98,.7)'
            },
            legend: {
                left: 'left',
                top: 'top',
                data: ['勘探', '发掘'],
                textStyle: {
                    color: 'rgba(135, 96, 63,.8)',
                    fontSize: 14
                }
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: {
                        type: ['line', 'bar'],
                        iconStyle: {
                            borderColor: '#701618'
                        },
                        emphasis: {
                            iconStyle: {
                                borderColor: '#AF8962'
                            }
                        }
                    },
                    restore: {
                        iconStyle: {
                            borderColor: '#701618'
                        },
                        emphasis: {
                            iconStyle: {
                                borderColor: '#AF8962'
                            }
                        }
                    }
                },
                itemSize: 14
            },
            color: ['#BA3732', '#E29B27']
        }
    },
    /**
     * 构造总统计数据
     * @param msg
     * @param callback
     */
    constructTotalStatisticsData(msg, callback) {
        let xData = [], yData = [];
        let legendList = [];
        for (const statusKey in status) {
            if (status.hasOwnProperty(statusKey)) {
                const {result, count} = this.checkStatus(msg, statusKey);
                xData.push(status[statusKey]);
                yData.push(count);
                if (result) {
                    legendList.push({name: status[statusKey], value: statusKey, count})
                }
            }
        }
        let totalStatistics = {xData, yData};
        callback(totalStatistics);
        store.dispatch({type: 'legendList', payload: legendList})
    },
    /**
     * 构造区域分布统计数据
     * @param msg
     * @param callback
     */
    constructCountryStatisticsData(msg, callback) {
        let countryStatistics = [];
        for (const statusKey in status) {
            if (status.hasOwnProperty(statusKey)) {
                const {count} = this.checkStatus(msg, statusKey);
                countryStatistics.push({name: status[statusKey], value: count})
            }
        }
        callback(countryStatistics);
    },
    /**
     * 获取数量
     * @param msg
     * @param status
     * @returns {{result: boolean, count: number}}
     */
    checkStatus(msg, status) {
        let result = false;
        let count = 0;
        for (let i = 0; i < msg.length; i++) {
            if (parseInt(msg[i].name) === parseInt(status)) {
                count = msg[i].count;
                result = true;
            }
        }
        return {result, count};
    },
    /**
     * 获取状态总数
     * @param year
     * @param country
     * @param callback
     */
    getTotalStatusData(year, country, callback) {
        commonConfig.validateTokenLoadData('project/count/genZTcount', {
            year, country
        }, msg => {
            if (msg && msg.code === '200') {
                const {KTZT, FJZT} = msg.data;
                callback(KTZT, FJZT)
            }
        });
    },
    /*获取所有区域*/
    getCountyDataList(callback) {
        commonConfig.ajaxNoFileMethods('project/count/getCountry', null, msg => {
            callback(msg.data || []);
        });
    },
    /*获取所有年份*/
    getYearDataList(callback) {
        commonConfig.ajaxNoFileMethods('project/count/getYear', null, msg => {
            callback(msg.data || []);
        });
    },
    /**
     * 构造月份统计数据
     * @returns {{fj: [], kt: []}}
     * @param ktData
     * @param fjData
     */
    constructMonthStatisticsData(ktData, fjData) {
        let month = commonConfig.options.month;
        let fj = [], kt = [];
        for (let i = 0; i < month.length; i++) {
            kt.push(this.checkStatus(ktData, month[i]).count);
            fj.push(this.checkStatus(fjData, month[i]).count);
        }
        return {fj, kt}
    },
    /**
     * 构造柱状图统计数据
     * @returns {{xData: [], yData: []}}
     * @param msg
     */
    constructBarStatisticsData(msg) {
        let xData = [], yData = [];
        for (let i = 0; i < msg.length; i++) {
            xData.push(msg[i].name)
            yData.push(msg[i].count)
        }
        return {xData, yData};
    }
}
export default statisticsMethods;